import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  //状态
  state: {
    length: 0,
    list: []
  },
  //变更状态
  mutations: {
    updateLength (state, payload) {
      state.length = payload
    },
    updateList (state, payload) {
      state.list = payload
    }

  },
  actions: {
    async queryData (context, payload) {
      console.log(payload);
      const ret = await axios.get('http://test.zjie.wang/tab')
      context.commit('updateLength', ret.data.list.length)
      context.commit('updateList', ret.data.list)
    }

  },
  getters: {
    favoriteList (state) {
      const ret = state.list.filter((item, index) => {
        return index < 2
      })
      return ret
    }
  }

})
